<!-- components/GlobalUnfollowDialog.vue -->
<template>
  <teleport to="body">
    <transition name="fade">
      <div v-if="visible" class="global-dialog-overlay" @click.self="close">
        <div class="global-dialog-content">
          <h3>取消关注</h3>
          <p>确定要取消关注「{{ currentItem?.name || '该用户' }}」吗？</p>
          <div class="dialog-actions">
            <button @click="close">取消</button>
            <button @click="confirm">确定</button>
          </div>
        </div>
      </div>
    </transition>
  </teleport>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'

interface Item {
  id: number
  isFollow: boolean
  name?: string
  [key: string]: any
}

const visible = ref(false)
const currentItem = ref<Item | null>(null)
let resolvePromise: ((value: boolean) => void) | null = null

const open = (item: Item): Promise<boolean> => {
  currentItem.value = item
  visible.value = true
  return new Promise((resolve) => {
    resolvePromise = resolve
  })
}

const close = () => {
  visible.value = false
  if (resolvePromise) {
    resolvePromise(false)
    resolvePromise = null
  }
}

const confirm = () => {
  visible.value = false
  if (resolvePromise) {
    resolvePromise(true)
    resolvePromise = null
  }
}

// 暴露方法给其他组件使用
defineExpose({ open, close })
</script>

<style scoped>
.global-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.global-dialog-content {
  background: white;
  padding: 20px;
  border-radius: 8px;
  min-width: 300px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>